home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / palis102.lha / Palis / cdxMakePatches / cdxMakePatches.h < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  169 lines

  1. /*
  2.     ·C·O·D·E·X· ·D·E·S·I·G·N· ·S·O·F·T·W·A·R·E·
  3.     presents:
  4.  
  5.     cdxMakePatches.h
  6.  
  7.     Here is some code on how I think patching libraries might be
  8.     as save as possible:
  9.  
  10.     - cdxPutFunction() will automatically track your patches and thus
  11.       you won't need to make several calls to SetFunction() to remove
  12.       your patches. cdxRemAllFunctions() will do that for you.
  13.     - cdxRemAllFunctions() is able to remove all patches.
  14.       In the case there do not occur problems (no overwritten patches,
  15.       SaferPatches active) it will simply remove them.
  16.       Otherwise it will return FALSE.
  17.       Moreover if you use the flag CDXFUNCF_ADDCODE when calling
  18.       cdxPutFunction() the object can also remove your functions without
  19.       direct danger for the system. A dummy code will be installed then.
  20.  
  21.     Suggested way of work:
  22.  
  23.     a) call cdxPutFunction(&ptr,CDXFUNCF_ADDCODE,,,,) for all patches.
  24.         (don't forget to initialize "ptr" by zero !)
  25.     b) do your job(s)
  26.     c) call cdxRemAllFunctions(&ptr,FALSE) to try to remove your tool
  27.         on the most friendly (I suggest the only acceptable) way.
  28.     d) if it returns FALSE, warn the user that some vectors can't be
  29.         restored execpt you waste memory when quitting your program.
  30.     e)    if the user still wants to remove your tool, call
  31.         cdxRemAllFunctions(&ptr,TRUE) and everything will be fine.
  32.         Of course, the dummy-code will only be left in memory if it neccessary
  33.         (e.g. the vector had been overwritten).
  34.  
  35.     For further flames call me up codex@stern.mathematik.hu-berlin.de or
  36.     buehlhan@kadewe.artcom.de or codex@kadewe.artcom.de.
  37.  
  38.     BTW this code might be used in any way you want.
  39.  
  40.     (w)2.1.1996 - happy new year to all readers !
  41. */
  42.  
  43. #include    <exec/nodes.h>
  44. #include    <exec/types.h>
  45. #include    <exec/memory.h>
  46. #include    <exec/semaphores.h>
  47. #include    <proto/exec.h>            // for SAS
  48.  
  49. // ------------------------------------
  50. // defines
  51. // ------------------------------------
  52.  
  53. #define    CDXFUNCF_ADDCODE    0x0001
  54.         // generate addtitional dummy code: If you use this, removing this
  55.         // function will always be possible even if the vector had been
  56.         // modified (though you waste about 10 bytes of memory)
  57. #define    CDXFUNCF_ADDSEM    0x0002
  58.         // allocate and initialize an additional struct SignalSemaphore *
  59.         // for your function.
  60.         // The address of it can be achieved by cdxGetPatchSem().
  61.         // use it this way in your custom funcion:
  62.         //
  63.         // ANYTYPE newfunc(...)
  64.         // {
  65.         //    ObtainSemaphoreShared(sem);
  66.         //
  67.         //    ret = ... do what you want ...
  68.         //
  69.         //    ReleaseSemaphore(sem);
  70.         //
  71.         //    return ret;
  72.         // }
  73.         //
  74.         // If you're now going to remove the function, cdxRemAllFunctions()
  75.         // will first remove your function then try to ObtainSemaphore(sem)
  76.         //  This way if any tasks were running your function
  77.         // cdxRemAllFunctions() will wait until these tasks left your func.
  78.         // Note that _you_ should even  dos/Delay(2)  after having called
  79.         // cdxRemAllFunctions() to ensure that the last two lines have also been
  80.         // executed !
  81.         // Note that you should use this way if your new function does heavy
  82.         // processing. If it is a fast one, forget it - it would be time-
  83.         // wasting !
  84.         // Note 2: Note that you cannot use this flag for
  85.         // exec/ObtainSemaphore(),exec/ObtainSemaphoreShared(),exec/Release-
  86.         // Semaphore(),GetMsg(),PutMsg(),ReplyMsg(),Wait() etc !!!!
  87.  
  88. // ------------------------------------
  89. // functions
  90. // ------------------------------------
  91.  
  92. /*
  93.  * cdxPutFunction()
  94.  * ----------------
  95.  *
  96.  * -- JOB --
  97.  * This function will SetFunction() your new function. It will - if specified
  98.  * using CDXFUNCF_ADDCODE generate a little dummy code which will be left
  99.  * in memory if it is neccessary to do so to remove all functions and if you
  100.  * want it by yourself.
  101.  * -- INPUTS --
  102.  * headPtr must be initialized by zero on first call.
  103.  * Flags see above.
  104.  * rest see dox for SetFunction()
  105.  * -- RESULT --
  106.  * Ptr to old vector or NULL (error)
  107.  * Note that further patches of you are still active !!!!!
  108.  *
  109.  * Note 1: *headPtr _MUST_ be set to zero before you do anything.
  110.  * Note 2: Since cdxPutFunction() uses Forbid(),Permit() and SetFunction()
  111.  * it wouldn't be wise to patch these exec functions using these functions ;-)
  112.  */
  113.  
  114. extern APTR __asm cdxPutFunction(register __a0 APTR    *headPtr,
  115.                                  register __d0 UWORD    Flags,
  116.                                  register __a1 APTR    lib,
  117.                                  register __d1 WORD    offset,
  118.                                  register __a2 APTR    newFunc);
  119.  
  120. /*
  121.  * cdxRemAllFunctions()
  122.  *
  123.  * -- JOB --
  124.  * This function will SetFunction() each of your old functions
  125.  * you set using cdxPutFunction(). It will check whether the code returned
  126.  * by SetFunction() matches the expected (your "NewFunc"s). In this case
  127.  * everything is fine and TRUE is returned. Otherwise, at last one of your
  128.  * vectors had been modified.
  129.  * If you use the "force" option all patches that have been installed using
  130.  * the CDXFUNCF_ADDCODE flag will be removed even if the library vector has
  131.  * been overwritten and a little dummy code will be left in memory (10 bytes
  132.  * per function the can't be removed ordinary).
  133.  * -- INPUTS --
  134.  * headPtr - see cdxPutFunction(). Might still be zero.
  135.  * force - see JOB above.
  136.  * -- RESULT --
  137.  * TRUE or FALSE. Note that even if you "force"d to uninstall your patches
  138.  * cdxRemAllFunctions() will reject to do so if a vector of a patch that
  139.  * hasn't been installed using CDXFUNCF_ADDCODE had been found modified.
  140.  *
  141.  * Note: If this func returns FALSE you can be sure that all your functions
  142.  * are still at work !!!!!!!!!!!!!!!
  143.  */
  144.  
  145. extern BOOL __asm cdxRemAllFunctions(register __a0 APTR    *headPtr,
  146.                                      register __d0 BOOL    force);
  147.  
  148. /*
  149.  *    cdxGetPatchSem()
  150.  * ----------------
  151.  *
  152.  * -- JOB --
  153.  * This functions might be used to get the semaphore allocated for a
  154.  * function by cdxPutFunction(...,CDXFUNCF_ADDSEM,...)
  155.  * See explanation of CDXFUNCF_ADDSEM for more information.
  156.  * -- INPUTS --
  157.  * headPtr - see cdxPutFunction(). Might still be zero (=> returns zero)
  158.  * newFunc - the address of the function you wanted a semaphore for.
  159.  * Might be zero (in this case the sem of the last patch will be used,
  160.  * even if you didn't wanted a semaphore (=>NULL)) !
  161.  * -- RESULT --
  162.  * Address or NULL. This function won't fail if there's a semaphore.
  163.  */
  164.  
  165. extern struct SignalSemaphore *__asm cdxGetPatchSem(register __a0 APTR *headPtr,
  166.                                                    register __a1 APTR newFunc);
  167.  
  168. /* (w)hans bühler */
  169.